// pages/loadgroup_manager/loadgroup_manager.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cur_district:'',
    cur_district_str:'',
    loadgroup_list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      cur_district:JSON.parse(options.district_info),
      cur_district_str: options.district_info
      //loadgroup_list:JSON.parse(options.loadgroup_list)
    });

    //console.log(that.data.cur_district, "cur_district");
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    wx.request({
      url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.cur_district.id,
      method:'post',
      header:header,
      success:res=>{

        
        // {"data":"[{\"attributes\":{\"mid\":1,\"type\":1},\"id\":200000001,\"text\":\"测试路段\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":4,\"type\":1},\"id\":200000004,\"text\":\"测试路段2\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":6,\"type\":1},\"id\":200000006,\"text\":\"测试路段3\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"}]"}
        //console.log(res, "list_loadgroup loadTree");
        let data = JSON.parse(res.data.data);

        if(data.length == 0){
          return;
        }

        that.setData({
          loadgroup_list:data
        });            
      },
      fail:error=>{

      }
    })
  },

  add_loadgroup:function(){
    let that = this;
    wx.navigateTo({
      url: '../new_group/new_group?district_info=' + that.data.cur_district_str
    })
  },

  mod_roadgroup:function(e){
    let that = this;
    //console.log(e, 'e')
    let mid = e.currentTarget.dataset.mid;
    let id = e.currentTarget.dataset.id;
    let districtid = e.currentTarget.dataset.districtid;
    let districtmid = e.currentTarget.dataset.districtmid;
    //let item = e.currentTarget.dataset.item;

    wx.navigateTo({
      url: '../edit_roadgroup/edit_roadgroup?mid=' + mid + '&id=' + id + '&districtmid=' + districtmid + '&districtid=' + districtid
    });

  },

  del_loadgroup:function(e){
    let that = this;
    //DeleteRoadGroup

    let mid = e.currentTarget.dataset.mid;
    let id = e.currentTarget.dataset.id;

    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    //{"error":"0","info":"删除成功"}
    wx.request({
      url:'https://119.3.46.145/server/DeleteRoadGroup?userid='+app.globalData.userid+'&id='+mid,
      header:header,
      method:'post',
      success:res=>{
        //console.log(res);
        wx.showModal({
          title: '提示',
          content: res.data.info,
          success:function(){
            if(res.data.error == '0'){ //删除成功
                //重新setData数据            
              let tmp_loadgroup = that.data.loadgroup_list;
                try {
                  tmp_loadgroup.forEach(function (item, index, self) {
                    if (item.id == id) {  //id指那一长串数字 100000001 20000001
                      self.splice(index, 1);
                      // console.log(index, "index");
                      throw new Error("ending");//报错，就跳出循环
                    }
                    // console.log("test foreach");
                  });
                } catch (e) {
                  if (e.message == "ending") {
                    // log("结束了");
                  }
                }

                that.setData({
                  loadgroup_list: tmp_loadgroup
                });
            }
          }
        })

      },
      fail:error=>{
        wx.showModal({
          title: '提示',
          content: 'error',
          showCancel:false
        })

      }

    });


  },

  list_loadgroup: function (e) {
    let that = this;

    // that.setData({
    //   mid: options.mid,
    //   id: options.id,
    //   districtid: options.districtid,
    //   districtmid: options.districtmid
    // });

    let mid = e.currentTarget.dataset.mid;
    let id = e.currentTarget.dataset.id;
    let districtid = e.currentTarget.dataset.districtid;
    let districtmid = e.currentTarget.dataset.districtmid;
    // let districtid = e.currentTarget.dataset.districtid;
    // let districtmid = e.currentTarget.dataset.districtmid;
    let loadName = e.currentTarget.dataset.loadname;  

    wx.navigateTo({
      url: '../lamp_manager/lamp_manager?loadgroupmid=' + mid + "&loadgroupid=" + id + '&districtid=' + districtid + '&districtmid=' + districtmid + '&districtname=' + that.data.cur_district.text + '&loadname='+loadName
    });
  },

  //更新name
  changeData: function (obj) {    
    let that = this;
    let cur_loadgroup = obj;
   

    //区域列表loadgroup_list数据  //{"data":"[{\"attributes\":{\"mid\":1,\"type\":1},\"id\":200000001,\"text\":\"测试路段\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":4,\"type\":1},\"id\":200000004,\"text\":\"测试路段2\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":6,\"type\":1},\"id\":200000006,\"text\":\"测试路段3\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"}]"}

    //obj数据{
  //   loadgroupName: e.detail.value.input_loadgroup,
  //   describe: e.detail.value.textarea,
  //   name: e.detail.value.input_name,
  //   telephone: e.detail.value.input_telephone,
  //   longitude: long_lati_arr[0],
  //   latitude: long_lati_arr[1]
  // }

    loadgroup_list.forEach(function (item, index, self) {
      if (item.id == id) {
        item.points.forEach(function (_item, _index, _self) {
          if (_item.devNo == devNo) {
            _item.devName = devName;
            return false;
          }
        });
        return false;
      }
    });

    that.setData({
      dev_list: tmp_list
    });

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})